import Vue from "vue";
import Vuex from "vuex";

Vue.use(Vuex);

import moduleA from "./moduleA";
import moduleB from "./moduleB";
export default new Vuex.Store({
  state: {
    name: "张三",
    age: 999,
    msg: "这是root的msg",
  },
  getters: {},
  mutations: {
    changeAge(state, payload) {
      console.log("changeAge");
      state.age = payload;
    },
    changeName(state, payload) {
      console.log("changeName");
      state.name = payload;
    },
    changeMsg(state, payload) {
      console.log("changeMsg");
      state.msg = payload;
    },
  },
  actions: {
    // 第一个参数是上下文 包含了commit 等一些方法
    // changeNameAction(,payload) {
    //   console.log(context)
    //   console.log("触发action")
    // }
    // changeNameAction(ctx,payload) {
    //   console.log(ctx)
    //   ctx.commit("changeName",payload)
    //   console.log("触发action")
    // }
    changeNameAction({ commit }, payload) {
      commit("changeName", payload);
      console.log("触发action");
    },
  },
  modules: { moduleA, moduleB },
});
